package com.ruoyi.rsikManage.MonitorPoint.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.rsikManage.MonitorPoint.domain.TMonitorPointRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rsikManage.MonitorPoint.domain.TMonitorOfflineStatistics;
import com.ruoyi.rsikManage.MonitorPoint.service.ITMonitorOfflineStatisticsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通道离线记录Controller
 *
 * @author sxygsj
 * @date 2024-12-25
 */
@RestController
@RequestMapping("/riskManage/monitorOffline")
public class TMonitorOfflineStatisticsController extends BaseController
{

    private final ITMonitorOfflineStatisticsService tMonitorOfflineStatisticsService;
    private final TokenService tokenService;

    public TMonitorOfflineStatisticsController(ITMonitorOfflineStatisticsService tMonitorOfflineStatisticsService,
                                               TokenService tokenService) {
        this.tMonitorOfflineStatisticsService = tMonitorOfflineStatisticsService;
        this.tokenService = tokenService;
    }


    /**
     * 查询通道离线记录列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:list')")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo list(HttpServletRequest httpServletRequest,
                              @RequestBody(required = false) TMonitorPointRequest tMonitorPointRequest)
    {

        // 初始化单位代码列表
        List<Long> unitCodes = new ArrayList<>();

        // 如果请求体不为空，则从请求体中获取单位代码
        if (tMonitorPointRequest != null) {
            unitCodes = tMonitorPointRequest.getUnitCodes();
        }

        // 如果单位代码列表为空，则尝试从当前登录用户获取单位代码
        if (CollectionUtils.isEmpty(unitCodes)) {
            if (httpServletRequest != null && tokenService.getTokenInfo(httpServletRequest) != null) {
                unitCodes = tokenService.getLoginUserUnitCodes(httpServletRequest);
            }
        }

        // 设置最终确定的单位代码到请求参数中
        if (tMonitorPointRequest == null) {
            tMonitorPointRequest = new TMonitorPointRequest();
        }
        tMonitorPointRequest.setUnitCodes(unitCodes);
        startPage();
        List<TMonitorOfflineStatistics> list = tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsList(tMonitorPointRequest);
        return getDataTable(list);
    }

    /**
     * 统计通道离线记录列表 年月日 类型
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:list')")
    @RequestMapping(value = "/count", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo hour(HttpServletRequest httpServletRequest,
                               @RequestBody(required = false) TMonitorPointRequest tMonitorPointRequest)
    {

        // 初始化单位代码列表
        List<Long> unitCodes = new ArrayList<>();

        // 如果请求体不为空，则从请求体中获取单位代码
        if (tMonitorPointRequest != null) {
            unitCodes = tMonitorPointRequest.getUnitCodes();
        }

        // 如果单位代码列表为空，则尝试从当前登录用户获取单位代码
        if (CollectionUtils.isEmpty(unitCodes)) {
            if (httpServletRequest != null && tokenService.getTokenInfo(httpServletRequest) != null) {
                unitCodes = tokenService.getLoginUserUnitCodes(httpServletRequest);
            }
        }

        // 设置最终确定的单位代码到请求参数中
        if (tMonitorPointRequest == null) {
            tMonitorPointRequest = new TMonitorPointRequest();
        }
        tMonitorPointRequest.setUnitCodes(unitCodes);
//        List<TMonitorOfflineStatistics> list = tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsMonth();
        List<Map<String,Object>> list = new ArrayList<>();
        String countType = tMonitorPointRequest.getCountType();
        if (countType!=null && countType.equals("年")){
             list = tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsYear(tMonitorPointRequest);
        } else if(countType!=null &&countType.equals("月")){
            list = tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsMonth(tMonitorPointRequest);
        }else {
            list = tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsHour(tMonitorPointRequest);
        }



        return getDataTable(list);
    }


    /**
     * 导出通道离线记录列表
     */
    @PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:export')")
    @Log(title = "通道离线记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMonitorPointRequest tMonitorPointRequest)
    {
        List<TMonitorOfflineStatistics> list = tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsList(tMonitorPointRequest);
        ExcelUtil<TMonitorOfflineStatistics> util = new ExcelUtil<TMonitorOfflineStatistics>(TMonitorOfflineStatistics.class);
        util.exportExcel(response, list, "通道离线记录数据");
    }

    /**
     * 获取通道离线记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tMonitorOfflineStatisticsService.selectTMonitorOfflineStatisticsById(id));
    }

    /**
     * 新增通道离线记录
     */
    @PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:add')")
    @Log(title = "通道离线记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonitorOfflineStatistics tMonitorOfflineStatistics)
    {
        return toAjax(tMonitorOfflineStatisticsService.insertTMonitorOfflineStatistics(tMonitorOfflineStatistics));
    }

    /**
     * 修改通道离线记录
     */
    @PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:edit')")
    @Log(title = "通道离线记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonitorOfflineStatistics tMonitorOfflineStatistics)
    {
        return toAjax(tMonitorOfflineStatisticsService.updateTMonitorOfflineStatistics(tMonitorOfflineStatistics));
    }

    /**
     * 删除通道离线记录
     */
    @PreAuthorize("@ss.hasPermi('rsikManage:monitorOffline:remove')")
    @Log(title = "通道离线记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMonitorOfflineStatisticsService.deleteTMonitorOfflineStatisticsByIds(ids));
    }
}
